package com.shwread.httpsdk.http.base;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import logic.constants.Const;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.MultipartPostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpStatus;

import android.content.Context;

import com.shwread.httpsdk.bean.FormFile;
import com.shwread.httpsdk.http.callback.ActionListener;
import com.shwread.httpsdk.util.NetUtil;


/**
 * @Title: AbstractHttpURLConnection.java
 * @Package com.surfingread.httpsdk.http.base
 * @Description: TODO(添加描述)
 * @author 刘章
 * @date 2014年2月19日 下午7:34:59
 */
public abstract class AbstractHttpURLPostForFile
        extends
        AbstractHttpBase
{
	private HashMap<String, String> parameter_map = new HashMap<String, String>();
	
	private HttpURLConnection       httpConnection;
	private InputStream             inStream;
	private DataOutputStream        outStream;
	private String                  action;
	private File                    file;
	private FormFile                formFile;
	
	public AbstractHttpURLPostForFile(Context mContext, String action, ActionListener actionListener) {
		super(mContext, action, actionListener);
		this.action = action;
	}
	
	protected abstract void getParameter(HashMap<String, String> parameter_map);
	
	protected abstract File getFile();
	
	public abstract void doResponseXml(String xml);
	
	public String getUrl() {
		return Const.SERVER_URL + "/" + action;
	}
	
	@SuppressWarnings ("deprecation")
	@Override
	protected void doHttp() {
		try
		{
			HttpClient httpClient = new HttpClient();
			@SuppressWarnings ("deprecation")
			MultipartPostMethod post = new MultipartPostMethod(getUrl());
			getParameter(parameter_map);
			if (parameter_map != null && !parameter_map.isEmpty())
			{
				Iterator<Entry<String, String>> its = parameter_map.entrySet().iterator();
				while (its.hasNext())
				{
					Entry<String, String> entry = its.next();
					post.addParameter(entry.getKey(), entry.getValue());
				}
			}
			file = getFile();
			post.addParameter("imageData", "temp.jpeg", file);
			post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
			
			int code = httpClient.executeMethod(post);
			println("code:" + code);
			
			if (code == HttpStatus.SC_OK)
			{
				doResponseXml(post.getResponseBodyAsString());
			}
			else
			{
				HTTPERROR(code, null);
			}
		}
		catch (Exception e)
		{
			HTTPERROR(NetUtil.NetError, e);
		}
		finally
		{
			DoDispose();
		}
	}
	
	public void DoDispose() {
		if (file != null)
		{
			file = null;
		}
		if (formFile != null)
		{
			formFile = null;
		}
		if (inStream != null)
		{
			try
			{
				inStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			inStream = null;
		}
		if (outStream != null)
		{
			try
			{
				outStream.close();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			outStream = null;
		}
		if (httpConnection != null)
		{
			httpConnection.disconnect();
			httpConnection = null;
		}
		if (header_map != null)
		{
			header_map.clear();
			header_map = null;
		}
		if (listener != null)
		{
			listener = null;
		}
		dispose();
		context = null;
	}
	
}
